package org.jetlinks.community.mstn.entity.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import org.hswebframework.ezorm.core.utils.StringUtils;
import org.hswebframework.ezorm.rdb.mapping.annotation.ColumnType;
import org.hswebframework.ezorm.rdb.mapping.annotation.DefaultValue;
import org.hswebframework.ezorm.rdb.mapping.annotation.EnumCodec;
import org.hswebframework.ezorm.rdb.mapping.annotation.JsonCodec;
import org.hswebframework.web.crud.generator.Generators;
import org.hswebframework.web.system.authorization.api.entity.UserEntity;
import org.hswebframework.web.validator.CreateGroup;
import org.hswebframework.web.validator.UpdateGroup;
import org.hswebframework.web.validator.ValidatorUtils;
import org.jetlinks.community.auth.entity.OrganizationEntity;
import org.jetlinks.community.auth.entity.UserDetail;
import org.jetlinks.community.auth.entity.UserDetailEntity;
import org.jetlinks.community.auth.enums.OrgType;
import org.jetlinks.community.auth.enums.RoleState;
import org.jetlinks.community.auth.enums.RoleType;
import org.jetlinks.community.auth.service.request.SaveUserRequest;
import org.jetlinks.community.mstn.enums.ProjectType;

import javax.persistence.Column;
import javax.validation.constraints.*;
import java.sql.JDBCType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Data
public class TenantCreateReqVO {

    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "租户ID")
    private String orgId;

    @Schema(description = "租户套餐ID")
    private String roleId;

    @Schema(description = "租户名称")
    private String name;

    @Schema(description = "联系人")
    private String contactName;

    @Schema(description = "联系人手机号,可为空")
    private String contactPhone;

    @Schema(description = "用户账号")
    private String username;

    @Schema(description = "说明")
    private String describe;

    @Schema(description = "密码")
    private String password;

    @Schema(description = "其他配置")
    private Map<String, Object> properties;


    @Schema(description = "过期时间", accessMode = Schema.AccessMode.READ_WRITE)
    private Long expireTime;


    public OrganizationEntity getOrganization(){
        OrganizationEntity organization = new OrganizationEntity();
        if (!StringUtils.isNullOrEmpty(getOrgId())) {
            organization.setId(getOrgId());
        }
        organization.setOrgType(OrgType.project);
        organization.setExpireTime(getExpireTime());
        organization.setName(getName());
        organization.setDescribe(getDescribe());
        organization.setProjectType(ProjectType.collect.getValue());
        organization.setProperties(getProperties());
        organization.setPackageId(getRoleId());
        return organization;
    }

    public UserDetail getUserDetail(){
        UserDetail detail = new UserDetail();
        detail.setId(getUserId());
        detail.setUsername(getUsername());
        detail.setPassword(getPassword());
        detail.setType(RoleType.tenant.getValue());
        detail.setName(getContactName());
        detail.setTelephone(getContactPhone());
        return detail;
    }


    public SaveUserRequest getSaveUserRequest(String orgId){
        SaveUserRequest request = new SaveUserRequest();
        request.setRoleIdList(new HashSet<>(Collections.singletonList(getRoleId())));
        request.setOrgIdList(new HashSet<>(Collections.singletonList(orgId)));
        request.setUser(getUserDetail());
        return request;
    }

}
